/**
 * Created by zhangb on 2015/9/16.
 */
//IE 7 之前版本 创建XHR对象
function createXHR()
{
    if(typeof XMLHttpRequest != "undefined")
    {
        return new XMLHttpRequest();
    }else if( typeof ActiveXObject != "undefined") {
        if(typeof arguments.callee.activeXString != "string")
        {
            var versions = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"],
                i,len;
            for(i=0,len=versions.length;i<len;i++)
            {
                try{
                    new ActiveXObject(versions[i]);
                    arguments.callee.activeXString = versions[i];
                    break;
                } catch (ex){

                }
            }
        }
        return new ActiveXObject(arguments.callee.activeXString);

    }else{
        throw new Error("NO XHR object available.")
    }
}

//var xhr = createXHR();

//xhr.onreadystatechange = function()
//{
//    if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304)
//    {
//        alert(xhr.responseText);
//    }else{
//        alert("Request was unsuccessful:" + xhr.status);
//    }
//};
//
////xhr.open("get","index.php",false);
////xhr.send(null);
////xhr.abort();
//
//xhr.open("get","index.html",true);
//xhr.setRequestHeader("myHeader","myValue");
//xhr.send(null);
//get 请求 用于向服务器 查询某些信息 url 可以通过下面函数进行编码
function addURLParam(url,name,value)
{
    url += (url.indexOf("?") == -1 ? "?" : "&");
    url += encodeURIComponent(name) + "=" + encodeURIComponent(value);

    return url;
}

//表单序列化
function serialize(form)
{
    var parts = [],
        field = null,
        i,len, j,optLen,option,optValue;

    for( i=0,len=form.elements.length;i<len;i++)
    {
        field = form.elements[i];

        switch(field.type)
        {
            case "select-one":
            case "select-multiple":
            if(field.name.length)
            {
                for(j=0,optLen = field.options.length;j<optLen;j++)
                {
                    option = field.options[j];
                    if(option.selected)
                    {
                        optValue = "";
                        if(option.hasAttribute)
                        {
                            optValue = (option.hasAttribute("value")?option.value:option.text);
                        }else{
                            optValue = (option.attributes["value"].specified ? option.value:option.text);
                        }
                        parts.push(encodeURIComponent(field.name)+ "=" +encodeURIComponent(optValue));
                    }
                }
            }
                break;
            case undefined:
            case "file":
            case "submit":
            case "reset":
            case "button":
                break;
            case "radio":
            case "checkbox":
                if(!field.checked)
                {
                    break;
                }

            default :
                if(field.name.length)
                {
                    parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value));
                }
        }

    }
    return parts.join("&");
}

//post 请求 用于向服务器发送应该被保存的数据

function submitData()
{
    var xhr = createXHR();
    xhr.onreadystatechange = function()
    {
        if(xhr.readyState == 4)
        {
            if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304)
            {
                alert(xhr.responseText);
            }else{
                alert("Request was unsuccessful:" + xhr.status);
            }
        }
    };

    xhr.open("post","index.php",true);
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    var form = document.getElementById("user_info");
    xhr.send(serialize(form));
}

submitData();

